using System;
using System.Collections;
using System.ComponentModel.Design;
using System.IO;
using System.Resources;

namespace VSSDKAssist.Platform.Helpers
{
	/// <summary>
	/// Resx Helper class
	/// </summary>
	public static class ResxHelper
	{
		/// <summary>
		/// Gets the RESX data node by id.
		/// </summary>
		/// <param name="serviceProvider">The service provider.</param>
		/// <param name="resxFileName">Name of the RESX file.</param>
		/// <param name="textResourceId">The text resource id.</param>
		/// <returns></returns>
		public static ResXDataNode GetResxDataNodeById(IServiceProvider serviceProvider, string resxFileName, string textResourceId)
		{
			Guard.ArgumentNotNull(serviceProvider, "serviceProvider");
			Guard.ArgumentNotNullOrEmptyString(resxFileName, "resxFileName");
			Guard.ArgumentNotNullOrEmptyString(textResourceId, "textResourceId");

			ResXDataNode node = null;

			using(ResXResourceReader reader = new ResXResourceReader(resxFileName))
			{
				reader.BasePath = Path.GetDirectoryName(resxFileName);
				reader.UseResXDataNodes = true;

				IDictionaryEnumerator enu = reader.GetEnumerator();

				while(enu.MoveNext())
				{
					if(enu.Key.ToString().Equals(textResourceId))
					{
						node = enu.Value as ResXDataNode;
						break;
					}
				}

				reader.Close();
			}

			return node;
		}

		/// <summary>
		/// Gets the RESX data node by text value.
		/// </summary>
		/// <param name="serviceProvider">The service provider.</param>
		/// <param name="resxFileName">Name of the RESX file.</param>
		/// <param name="textResourceValue">The text resource value.</param>
		/// <returns></returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
		public static ResXDataNode GetResxDataNodeByTextValue(IServiceProvider serviceProvider, string resxFileName, string textResourceValue)
		{
			Guard.ArgumentNotNull(serviceProvider, "serviceProvider");
			Guard.ArgumentNotNullOrEmptyString(resxFileName, "resxFileName");
			Guard.ArgumentNotNullOrEmptyString(textResourceValue, "textResourceValue");

			ResXDataNode node = null;
			ITypeResolutionService typeResolutionService =
				serviceProvider.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;

			using(ResXResourceReader reader = new ResXResourceReader(resxFileName))
			{
				reader.BasePath = Path.GetDirectoryName(resxFileName);
				reader.UseResXDataNodes = true;

				IDictionaryEnumerator enu = reader.GetEnumerator();

				while(enu.MoveNext())
				{
					ResXDataNode nodeTemp = enu.Value as ResXDataNode;

					if(nodeTemp.GetValue(typeResolutionService).ToString().Equals(textResourceValue))
					{
						node = nodeTemp;
						break;
					}
				}

				reader.Close();
			}

			return node;
		}

		/// <summary>
		/// Gets the RESX text value by id.
		/// </summary>
		/// <param name="serviceProvider">The service provider.</param>
		/// <param name="resxFileName">Name of the RESX file.</param>
		/// <param name="textResourceId">The text resource id.</param>
		/// <returns></returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
		public static T GetResxValueById<T>(IServiceProvider serviceProvider, string resxFileName, string resourceId) where T:class
		{
			Guard.ArgumentNotNull(serviceProvider, "serviceProvider");
			Guard.ArgumentNotNullOrEmptyString(resxFileName, "resxFileName");
			Guard.ArgumentNotNullOrEmptyString(resourceId, "textResourceId");

			T resxValue = default(T);

			ITypeResolutionService typeResolutionService =
				serviceProvider.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;

			ResXDataNode node = GetResxDataNodeById(serviceProvider, resxFileName, resourceId);

			if(node != null)
			{
				resxValue = node.GetValue(typeResolutionService) as T;
			}

			return resxValue;
		}

		/// <summary>
		/// Gets the RESX id by text value.
		/// </summary>
		/// <param name="serviceProvider">The service provider.</param>
		/// <param name="resxFileName">Name of the RESX file.</param>
		/// <param name="textResourceValue">The text resource value.</param>
		/// <returns></returns>
		public static string GetResxIdByTextValue(IServiceProvider serviceProvider, string resxFileName, string textResourceValue)
		{
			Guard.ArgumentNotNull(serviceProvider, "serviceProvider");
			Guard.ArgumentNotNullOrEmptyString(resxFileName, "resxFileName");
			Guard.ArgumentNotNullOrEmptyString(textResourceValue, "textResourceValue");

			string resxId = null;
			ResXDataNode node = GetResxDataNodeByTextValue(serviceProvider, resxFileName, textResourceValue);

			if(node != null)
			{
				resxId = node.Name;
			}

			return resxId;
		}
	}
}